//
//  AddressTableViewCell.m
//  HuaRun2
//
//  Created by admin on 17/8/29.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "AddressTableViewCell.h"
#import "AddressCollectionViewCell.h"
#import "AddressModel.h"

@interface AddressTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property(nonatomic, strong)UICollectionView * collectionView;

@end

static NSString * const indentifier = @"UICollectionViewCell";

@implementation AddressTableViewCell

#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    self.contentView.backgroundColor = RGBA(249, 249, 249, 1);

    [self.contentView addSubview:self.collectionView];
}

- (void)layoutInit {
//    weakself(ws);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.addressArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AddressCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:indentifier
                                                                                 forIndexPath:indexPath];
//    if ([self.addressArray[indexPath.row] isKindOfClass: [AddressModel class]]) {
        cell.addressModel = self.addressArray[indexPath.row];
//    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    self.clickBlock ? self.clickBlock(indexPath.row) : nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_addressArray.count == 1) {
        return CGSizeMake(kMainScreenWidth - 20, 164);
    }
    return CGSizeMake(234, 164);
}

#pragma mark - getter setter 
- (void)setAddressArray:(NSArray *)addressArray {
    _addressArray = addressArray;
   
    [self.collectionView reloadData];
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];
        CGFloat itemWidth = 234;
        CGFloat itemHeight = 164;
        layout.itemSize = CGSizeMake(itemWidth, itemHeight);
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        self.collectionView = [[UICollectionView alloc] initWithFrame:(CGRectMake(10, 0, kMainScreenWidth - 20, 164))
                                                 collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
//        _collectionView.backgroundColor = [UIColor yellowColor];
        [_collectionView registerClass:[AddressCollectionViewCell class]
            forCellWithReuseIdentifier:@"UICollectionViewCell"];
        _collectionView.backgroundColor =  RGBA(249, 249, 249, 1);
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = NO;
    }
    return _collectionView;
}


@end
